package com.message.tcp.ip.ui.member.dialog;

import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.message.tcp.ip.socket.configure.entity.Member;
import com.message.tcp.ip.socket.configure.service.MemberService;
import com.message.tcp.ip.socket.util.UtilPackage;

public class MemberTable extends JTable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3991673841764266322L;
	private String[] columnName;
	private Object[][] data;
	private DefaultTableModel model;
	
	// business logic
	private MemberService memberService;
	public MemberTable(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		memberService = (MemberService) context.getBean("memberService");
		columnName = new String[]{"ID","IIN","MEMBER ID","NAME","LOCATION"};
		List<Member> members = memberService.getMembers();
		data = UtilPackage.membersToArray(members);
		model = new DefaultTableModel(data,columnName);
		this.setModel(model);
		this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumn colId = this.getColumnModel().getColumn(0);
		TableColumn colMem = this.getColumnModel().getColumn(1);
		TableColumn colIin = this.getColumnModel().getColumn(2);
		TableColumn colNam = this.getColumnModel().getColumn(3);
		TableColumn colLoc = this.getColumnModel().getColumn(4);
		colId.setPreferredWidth(40);
		colMem.setPreferredWidth(100);
		colIin.setPreferredWidth(100);
		colNam.setPreferredWidth(280);
		colLoc.setPreferredWidth(400);
	}
}
